package com.accountbooks.justin;


import java.util.HashMap;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabWidget;
import android.widget.TextView;

public class AccountBooksActivity extends FragmentActivity {
	private TabHost tabHost ;
	private TabManager tabManager;
	private TabWidget tabWidget;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        tabHost = (TabHost)this.findViewById(R.id.tabhost);
        tabHost.setup();
        Resources resources = this.getResources();
        tabManager = new TabManager(this, tabHost, android.R.id.tabcontent);
        tabManager.addTab(tabHost.newTabSpec("total").setIndicator(resources.getString(R.string.tabTotal)), TotalFragment.class, null);
        tabManager.addTab(tabHost.newTabSpec("inputting").setIndicator(resources.getString(R.string.tabInputting)), FoodFragment.class, null);
        tabManager.addTab(tabHost.newTabSpec("querying").setIndicator(resources.getString(R.string.tabQuerying)), TotalFragment.class, null);
        tabManager.addTab(tabHost.newTabSpec("setting").setIndicator(resources.getString(R.string.tabSetting)), TotalFragment.class, null);
        
        //设置Tab高度字体大小

        tabWidget = (TabWidget)this.findViewById(android.R.id.tabs);

        for (int i = 0; i < tabWidget.getChildCount();i++) {
	         View view = tabWidget.getChildTabViewAt(i);   
	         view.getLayoutParams().height = 50; //tabWidget.getChildAt(i)
	         final TextView tv = (TextView) view.findViewById(android.R.id.title);
	         //tv.setWidth(120);
	         tv.setTextSize(8);
        }

        if (savedInstanceState != null) {
        	tabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
    }
    @Override    
    protected void onSaveInstanceState(Bundle outState) {        
    	super.onSaveInstanceState(outState);       
    	outState.putString("tab", tabHost.getCurrentTabTag());    
    }
    
    public static class TabManager implements TabHost.OnTabChangeListener {
    	private final FragmentActivity mActivity;
    	private final TabHost mTabHost;
    	private final int mContainerId;
    	private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
    	TabInfo mLastTab;
    	
    	static final class TabInfo {
    		private final String tag;
    		private final Class<?> cls;
    		private final Bundle args;
    		private Fragment fragment;
    		
    		public TabInfo (String tag, Class<?> cls, Bundle args) {
    			this.tag = tag;
    			this.cls = cls;
    			this.args = args;
    		}
    	}
    	
    	static class DummyTabFactory implements TabContentFactory {
    		private final Context mContext;
    		
    		public DummyTabFactory(Context context) {
    			mContext =context;
    		}
    		
			public View createTabContent(String tag) {
				View view = new View(mContext);
				view.setMinimumHeight(0);
				view.setMinimumWidth(0);
				return view;
			}
    	}
    	
    	public TabManager(FragmentActivity activity, TabHost tabHost, int containerID) {
    		mActivity = activity;
    		mTabHost = tabHost;
    		mContainerId = containerID;
    		mTabHost.setOnTabChangedListener(this);
    	}
    	
    	public void addTab(TabHost.TabSpec tabSpec, Class<?> cls, Bundle args) {
    		tabSpec.setContent(new DummyTabFactory(mActivity));
    		String tag = tabSpec.getTag();
    		
    		TabInfo tabInfo = new TabInfo(tag, cls, args);
    		tabInfo.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);
    		if (tabInfo.fragment != null && ! tabInfo.fragment .isDetached()) {
    			FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
    			ft.detach(tabInfo.fragment);
    			ft.commit();
    		}
    		mTabs.put(tag, tabInfo);
    		mTabHost.addTab(tabSpec);
    	}
    	
		public void onTabChanged(String tabId) {
			TabInfo newTab = mTabs.get(tabId);
			if (mLastTab != newTab) {    
				
				FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();   
				
				if (mLastTab != null) {                    
					if (mLastTab.fragment != null) {                        
						 ft.detach(mLastTab.fragment);                    
					 }                
				 }   
				 
				 if (newTab != null) {                    
					 if (newTab.fragment == null) {                        
						 newTab.fragment = Fragment.instantiate(mActivity, newTab.cls.getName(), newTab.args);
						 ft.add(mContainerId, newTab.fragment, newTab.tag);                    
					} else {                        
						ft.attach(newTab.fragment);                    
					}                
				}

				 mLastTab = newTab;                
				 ft.commit();                
				 mActivity.getSupportFragmentManager().executePendingTransactions();            
			}
		}
    } 
}